package com.cf.auth.config;

import cn.hutool.core.bean.BeanUtil;
import com.cf.auth.module.SwordUserDetails;
import com.cf.auth.utils.TokenUtil;
import lombok.AllArgsConstructor;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * 令牌增强器
 * @author ChenFeng
 * @date 2021/11/8 6:13 下午
 * @description
 */
@AllArgsConstructor
public class SwordJwtTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication oAuth2Authentication) {
        // 获取用户信息
        SwordUserDetails principal = (SwordUserDetails) oAuth2Authentication.getPrincipal();
        Map<String, Object> info = new HashMap<>(16);
        info.put(TokenUtil.CLIENT_ID, TokenUtil.getClientIdFromHeader());
        info.put(TokenUtil.USER_ID, principal.getUserId());
        info.put(TokenUtil.DEPT_ID, principal.getDeptId());
        info.put(TokenUtil.POST_ID, principal.getPostId());
        info.put(TokenUtil.ROLE_ID, principal.getRoleId());
        info.put(TokenUtil.TENANT_ID, principal.getTenantId());
        info.put(TokenUtil.OAUTH_ID, "");
        info.put(TokenUtil.ACCOUNT, principal.getAccount());
        info.put(TokenUtil.USER_NAME, principal.getUsername());
        info.put(TokenUtil.NICK_NAME, "管理员");
        info.put(TokenUtil.REAL_NAME, principal.getRealName());
        info.put(TokenUtil.ROLE_NAME, "administrator");
        info.put(TokenUtil.AVATAR, "https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png");
        info.put(TokenUtil.DETAIL, "{type: \"web\"}");
        info.put(TokenUtil.LICENSE, TokenUtil.LICENSE_NAME);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
        return accessToken;
    }
}
